package edu.cdnu.common.result;

import java.io.Serializable;

import edu.cdnu.utils.ResourcesUtil;
/**
 * 包装Response的类
 * @author ASUS AsingZ
 * @param <T>  返回数据的类型
 * 
 */
public class ResultEntity<T> implements Serializable{
	private static final long serialVersionUID = 1L;
	/**
	 * 状态码
	 */
	private String code;
	/**
	 * 消息
	 */
	private String message;
	/**
	 * 数据
	 */
	private T data;
	
	public ResultEntity(String errorCode, String message, T data) {
		super();
		this.code = errorCode;
		this.message = message;
		this.data = data;
	}
	
	public String getCode() {
		return code;
	}
	public void setCode(String code) {
		this.code = code;
	}
	public String getMessage() {
		return message;
	}
	public void setMessage(String message) {
		this.message = message;
	}
	public T getData() {
		return data;
	}
	public void setData(T data) {
		this.data = data;
	}
	
	/**
	 * 创建成功的返回 code:200
	 * @param data 数据
	 * @return
	 */
	public  static<T>  ResultEntity<T> createSuccessResult(T data){
		ResultEntity<T> resultEntity = new ResultEntity<T>("200","成功",data);
		return resultEntity;
	}
	
	/**
	 * 创建错误的返回
	 * @param code 错误代码 MessageConstatnt接口中定义
	 * @return
	 */
	public  static<T> ResultEntity<T> createFailResult(Integer code){
		// 通过ResourcesUtil
		ResultEntity<T> resultEntity = new ResultEntity<T>(String.valueOf(code),ResourcesUtil.getMessageByCode(code),null);
		return resultEntity;
	}
	
	/**
	 * 创建成功的返回 code:200
	 * 重写，设置消息提示
	 * @param data 数据
	 * @return
	 */
	public  static<T>  ResultEntity<T> createSuccessResult(T data,String message){
		ResultEntity<T> resultEntity = new ResultEntity<T>("200",message,data);
		return resultEntity;
	}
}
